package com.cadre.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.common.utils.oldutil.PageUtils;
import com.cadre.common.utils.oldutil.Query;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.DIC_ZB;
import com.cadre.system.domain.officer.PersonInfor;
import com.cadre.system.mapper.A01Mapper;
import com.cadre.system.service.A04Service;
import com.cadre.system.service.DIC_ZBService;
import com.cadre.system.service.PersonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("personService")
public class PersonServiceImpl implements PersonService {
    @Autowired
    private A01Mapper a01Mapper;
    @Resource
    private DIC_ZBService dIC_ZBService;
    @Resource
    private A04Service a04Ser;

    @Override
    public PersonInfor info(String flowid) {
        PersonInfor pInfor = new PersonInfor();
        pInfor.setFlowid(flowid);
        if (flowid != null) {
            A01 a01s = this.a01Mapper.selectByPrimaryKey(flowid);
            if (a01s != null) {
                pInfor.setA0101(a01s.getA0101());
                pInfor.setA0105(a01s.getA0105());
                pInfor.setA0106(DateUtil.dateToStr(a01s.getA0106()));
                pInfor.setA0107(a01s.getA0107());
                pInfor.setA0108(a01s.getA0108());
                pInfor.setA0130(a01s.getA0130());
                pInfor.setA0133(a01s.getA0133());
                pInfor.setA0171(a01s.getA0171());
                pInfor.setA0175(a01s.getA0175());
                pInfor.setA0176(a01s.getA0176());
                pInfor.setA0177a(a01s.getA0177a());
                pInfor.setA0178(a01s.getA0178());
                pInfor.setA0179(a01s.getA0179());
                pInfor.setA0183(a01s.getA0183());
                pInfor.setA0184(DateUtil.dateToStr(a01s.getA0184()));
                pInfor.setA0184(a01s.getA0184a());
                pInfor.setA0180(DateUtil.dateToStr(a01s.getA0180()));
                pInfor.setA0193(a01s.getA0193());
                pInfor.setA0196(a01s.getA0196());
                pInfor.setA0197(a01s.getA0197());
            } else {
                System.out.println("---------------a01 null-----------------");
            }
        } else {
            System.out.println("---------------flowid null-----------------");
        }
        return pInfor;
    }

    @Override // com.tdqy.cadre.modular.profess.service.PersonService
    public PageInfo selectList(Map<String, Object> map) {
        String att1;
        //拼装ID
        String att = (String) map.get("ids");
        if (!(att == null || att.length() == 0)) {
            String[] ids = att.split(",");
            if (ids.length > 1000) {
                List<String> list = new ArrayList<>();
                for (int a = 0; a < 1000; a++) {
                    list.add(ids[a]);
                }
                map.put("ids", (String[]) list.toArray(new String[0]));
                List<String> list2 = new ArrayList<>();
                for (int a2 = 1000; a2 < ids.length; a2++) {
                    list2.add(ids[a2]);
                }
                map.put("ids2", (String[]) list2.toArray(new String[0]));
            } else {
                map.put("ids", ids);
            }
        }
        //拼装名字，flowids
        String att12 = map.get("name") == null ? "" : map.get("name").toString().trim();
        if (!(att12 == null || att12 == "" || (att1 = att12.replaceAll("，", ",").toUpperCase()) == null || att1.length() == 0)) {
            map.put("names", att1.split(","));
        }
        String atts = (String) map.get("flowids");
        if (!(atts == null || atts.length() == 0)) {
            map.put("flowids", atts.split(","));
        }
        //查询职级表
        Map<String, Object> mapZj = new HashMap<>();
        mapZj.put("tableName", "DIC_ZB1009");
        List<DIC_ZB> listZj = this.dIC_ZBService.getAll(mapZj);
        String fromRankNew = (String) map.get("A0198Start");
        String toRankNew = (String) map.get("A0198End");
        if (!(!(fromRankNew == null || fromRankNew.isEmpty()) || toRankNew == null || toRankNew.length() == 0)) {
            try {
                fromRankNew = listZj.get(listZj.size() - 1).getName();
            } catch (Exception e) {
            }
        }
        if (!(fromRankNew == null || fromRankNew.length() == 0 || !(toRankNew == null || toRankNew.isEmpty()))) {
            try {
                toRankNew = listZj.get(0).getName();
            } catch (Exception e2) {
            }
        }
        String rankNew = "";
        boolean startFlag = false;
        boolean stopFlag = false;
        if (!(fromRankNew == null || fromRankNew.length() == 0 || toRankNew == null || toRankNew.length() == 0)) {
            for (int i = 0; i < listZj.size(); i++) {
                if (listZj.get(i).getName().equals(fromRankNew)) {
                    startFlag = true;
                }
                if (startFlag && !stopFlag) {
                    rankNew = rankNew + listZj.get(i).getName() + ",";
                }
                if (listZj.get(i).getName().equals(toRankNew)) {
                    stopFlag = true;
                }
            }
            if (rankNew.equals("")) {
                rankNew = "";
                boolean startFlag2 = false;
                boolean stopFlag2 = false;
                for (int i2 = listZj.size() - 1; i2 >= 0; i2--) {
                    if (listZj.get(i2).getName().equals(fromRankNew)) {
                        startFlag2 = true;
                    }
                    if (startFlag2 && !stopFlag2) {
                        rankNew = rankNew + listZj.get(i2).getName() + ",";
                    }
                    if (listZj.get(i2).getName().equals(toRankNew)) {
                        stopFlag2 = true;
                    }
                }
            }
            //根据职级输出存入一个rank
            map.put("rank", rankNew.split(","));
        }
        //查询行政职务职级表
        Map<String, Object> mapZc = new HashMap<>();
        mapZc.put("tableName", "DIC_ZB09");
        List<DIC_ZB> listZc = this.dIC_ZBService.getAll(mapZc);
        String fromRank = (String) map.get("A0179Start");
        String toRank = (String) map.get("A0179End");
        if (!(!(fromRank == null || fromRank.isEmpty()) || toRank == null || toRank.length() == 0)) {
            try {
                fromRank = listZc.get(listZc.size() - 1).getName();
            } catch (Exception e3) {
            }
        }
        if (!(fromRank == null || fromRank.length() == 0 || !(toRank == null || toRank.isEmpty()))) {
            try {
                toRank = listZc.get(0).getName();
            } catch (Exception e4) {
            }
        }
        String rankTNew = "";
        boolean startFlag1 = false;
        boolean stopFlag1 = false;
        if (!(fromRank == null || fromRank.length() == 0 || toRank == null || toRank.length() == 0)) {
            for (int i3 = 0; i3 < listZc.size(); i3++) {
                if (listZc.get(i3).getName().equals(fromRank)) {
                    startFlag1 = true;
                }
                if (startFlag1 && !stopFlag1) {
                    rankTNew = rankTNew + listZc.get(i3).getName() + ",";
                }
                if (listZc.get(i3).getName().equals(toRank)) {
                    stopFlag1 = true;
                }
            }
            if (rankTNew.equals("")) {
                rankTNew = "";
                boolean startFlag12 = false;
                boolean stopFlag12 = false;
                for (int i4 = listZc.size() - 1; i4 >= 0; i4--) {
                    if (listZc.get(i4).getName().equals(fromRank)) {
                        startFlag12 = true;
                    }
                    if (startFlag12 && !stopFlag12) {
                        rankTNew = rankTNew + listZc.get(i4).getName() + ",";
                    }
                    if (listZc.get(i4).getName().equals(toRank)) {
                        stopFlag12 = true;
                    }
                }
            }
            //输出一个新rank
            map.put("rankNew", rankTNew.split(","));
        }
        String fromTime1 = (String) map.get("A0180Start");
        String toTime1 = (String) map.get("A0180End");
        if (fromTime1 == null || fromTime1.length() == 0 || toTime1 == null || toTime1.length() != 0) {
        }
        String fromTime2 = (String) map.get("A0142Start");
        String toTime2 = (String) map.get("A0142End");
        if (fromTime2 == null || fromTime2.length() == 0 || toTime2 == null || toTime2.length() != 0) {
        }
        //查学历
        Map<String, Object> map5 = new HashMap<>();
        map5.put("tableName", "DIC_XUELI");
        List<DIC_ZB> listXueli = this.dIC_ZBService.getAll(map5);
        String fromXueli = (String) map.get("A0405Start");
        String toXueli = (String) map.get("A0405End");
        if (!(!(fromXueli == null || fromXueli.isEmpty()) || toXueli == null || toXueli.length() == 0)) {
            try {
                fromXueli = listXueli.get(listXueli.size() - 1).getCode();
            } catch (Exception e5) {
            }
        }
        if (!(fromXueli == null || fromXueli.length() == 0 || !(toXueli == null || toXueli.isEmpty()))) {
            try {
                toXueli = listXueli.get(0).getCode();
            } catch (Exception e6) {
            }
        }
        String xueLiNew = "";
        boolean startFlag22 = false;
        boolean stopFlag22 = false;
        if (!(fromXueli == null || fromXueli.length() == 0 || toXueli == null || toXueli.length() == 0)) {
            for (int i5 = 0; i5 < listXueli.size(); i5++) {
                if (listXueli.get(i5).getCode().equals(fromXueli)) {
                    startFlag22 = true;
                }
                if (startFlag22 && !stopFlag22) {
                    xueLiNew = xueLiNew + listXueli.get(i5).getCode() + ",";
                }
                if (listXueli.get(i5).getCode().equals(toXueli)) {
                    stopFlag22 = true;
                }
            }
            if (xueLiNew.equals("")) {
                xueLiNew = "";
                boolean startFlag23 = false;
                boolean stopFlag23 = false;
                for (int i6 = listXueli.size() - 1; i6 >= 0; i6--) {
                    if (listXueli.get(i6).getCode().equals(fromXueli)) {
                        startFlag23 = true;
                    }
                    if (startFlag23 && !stopFlag23) {
                        xueLiNew = xueLiNew + listXueli.get(i6).getCode() + ",";
                    }
                    if (listXueli.get(i6).getCode().equals(toXueli)) {
                        stopFlag23 = true;
                    }
                }
            }
            map.put("xueli", xueLiNew.split(","));
        }
        String quanrizhi = (String) map.get("A0449");
        String isQuickQuery = (String) map.get("quickQuery");
        if (isQuickQuery == null || "Y".equals(isQuickQuery)) {
        }
        map.put("A0449", quanrizhi);
        try {
            String ageStart1 = (String) map.get("ageStart1");
            String ageEnd1 = (String) map.get("ageEnd1");
            if (ageStart1 != null && !ageStart1.isEmpty() && ageEnd1 != null && !ageEnd1.isEmpty() && ageStart1.equals(ageEnd1)) {
                map.put("ageStart", ageStart1);
                map.put("ageEnd", String.valueOf(Integer.parseInt(ageEnd1) + 1));
                map.remove("ageStart1");
                map.remove("ageEnd1");
            }
        } catch (Exception e7) {
        }
        try {
            String a0180Start = (String) map.get("A0180Start");
            String a0180End = (String) map.get("A0180End");
            if (a0180Start != null && !a0180Start.isEmpty() && a0180End != null && !a0180End.isEmpty() && a0180Start.equals(a0180End)) {
                map.put("A0180Start1", a0180Start);
                map.put("A0180End1", String.valueOf(Integer.parseInt(a0180End) + 1));
                map.remove("A0180Start");
                map.remove("A0180End");
            }
        } catch (Exception e8) {
        }
        try {
            String a0185Start = (String) map.get("A0185Start");
            String a0185End = (String) map.get("A0185End");
            if (a0185Start != null && !a0185Start.isEmpty() && a0185End != null && !a0185End.isEmpty() && a0185Start.equals(a0185End)) {
                map.put("A0185Start1", a0185Start);
                map.put("A0185End1", String.valueOf(Integer.parseInt(a0185End) + 1));
                map.remove("A0185Start");
                map.remove("A0185End");
            }
        } catch (Exception e9) {
        }
        try {
            String a0185Start2 = (String) map.get("A0142Start");
            String a0185End2 = (String) map.get("A0142End");
            if (a0185Start2 != null && !a0185Start2.isEmpty() && a0185End2 != null && !a0185End2.isEmpty() && a0185Start2.equals(a0185End2)) {
                map.put("A0142Start1", a0185Start2);
                map.put("A0142End1", String.valueOf(Integer.parseInt(a0185End2) + 1));
                map.remove("A0142Start");
                map.remove("A0142End");
            }
        } catch (Exception e10) {
        }
        PageHelper.startPage(Integer.parseInt(String.valueOf(map.get("pageNum"))), Integer.parseInt(String.valueOf(map.get("pageSize"))));
        List<A01> persons = this.a01Mapper.selectList(map);
        if (persons.size() > 0) {
            for (int i7 = 0; i7 < persons.size(); i7++) {
                String name = persons.get(i7).getA0101();
                if (StringUtils.isNotBlank(name) && name.length() == 2) {
//                    persons.get(i7).setA0101(name.charAt(0) + "&nbsp;&nbsp;&nbsp;&nbsp;" + name.charAt(1));
                    persons.get(i7).setA0101(name);
                }
            }
        }
        return new PageInfo(persons);
    }

    @Override // com.tdqy.cadre.modular.profess.service.PersonService
    public List<A01> selectListForExport(Map<String, Object> map) {
        String att1;
        String att = (String) map.get("ids");
        if (!(att == null || att.length() == 0)) {
            map.put("ids", att.split(","));
        }
        String att12 = (String) map.get("name");
        if (!(att12 == null || att12 == "" || (att1 = att12.replaceAll("，", ",").toUpperCase()) == null || att1.length() == 0)) {
            map.put("names", att1.split(","));
        }
        String atts = (String) map.get("flowids");
        if (!(atts == null || atts.length() == 0)) {
            map.put("flowids", atts.split(","));
        }
        PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
        List<A01> persons = this.a01Mapper.selectList(map);
        if (CollectionUtils.isEmpty(persons)) {
            return persons;
        }
        for (int i = 0; i < persons.size(); i++) {
            A01 vo = persons.get(i);
            String a0105 = vo.getA0105();
            boolean b = StringUtils.isNotEmpty(a0105) && "女".equals(a0105);
            String a0130 = vo.getA0130();
            boolean b1 = StringUtils.isNotEmpty(a0130) && !"汉族".equals(a0130);
            StringBuilder sb = new StringBuilder();
            if (b && !b1) {
                sb.append("(女)");
            }
            if (b && b1) {
                sb.append("(女,");
                sb.append(a0130);
                sb.append(")");
            }
            if (!b && b1) {
                sb.append("(");
                sb.append(a0130);
                sb.append(")");
            }
            if (StringUtils.isNotBlank(sb.toString())) {
                vo.setA0101(vo.getA0101() + "\n" + sb.toString());
            }
        }
        return persons;
    }

    @Override // com.tdqy.cadre.modular.profess.service.PersonService
    public String selectNextDetail(Map<String, Object> map) {
        String att1;
        String att = (String) map.get("ids");
        if (!(att == null || att.length() == 0)) {
            map.put("ids", att.split(","));
        }
        String att12 = (String) map.get("name");
        if (!(att12 == null || att12 == "" || (att1 = att12.replaceAll("，", ",").toUpperCase()) == null || att1.length() == 0)) {
            map.put("names", att1.split(","));
        }
        String flag = map.get("flag").toString();
        String pageSize = map.get("pageSize").toString();
        String page = map.get("pageNum").toString();
        String now = map.get("now").toString();
        int nows = 0;
        if (!(pageSize == null || pageSize == "" || page == null || page == "" || now == null || now == "")) {
            nows = Integer.parseInt(page) != 1 ? ((Integer.parseInt(page) - 1) * Integer.parseInt(pageSize)) + Integer.parseInt(now) : Integer.parseInt(now);
        }
        String id = null;
        A01 a01 = null;
        if (flag.equals("next")) {
            map.put("no", Integer.valueOf(nows + 1));
            a01 = this.a01Mapper.selectByRowNum(map);
        } else if (flag.equals("last")) {
            map.put("no", Integer.valueOf(nows - 1));
            a01 = this.a01Mapper.selectByRowNum(map);
        }
        if (a01 != null) {
            id = a01.getFlowid();
        }
        return id;
    }

    @Override // com.tdqy.cadre.modular.profess.service.PersonService
    @Transactional(rollbackFor = {Exception.class})
    public int insert(PersonInfor personInfor) {
        if (personInfor == null) {
            return 0;
        }
        A01 a01s = new A01();
        a01s.setFlowid(GenerateUUID.getUUID());
        a01s.setA0101(personInfor.getA0101());
        a01s.setA0105(personInfor.getA0105());
        a01s.setA0106(DateUtil.strToDate(personInfor.getA0106()));
        a01s.setA0107(personInfor.getA0107());
        a01s.setA0108(personInfor.getA0108());
        a01s.setA0130(personInfor.getA0130());
        a01s.setA0133(personInfor.getA0133());
        a01s.setA0171(personInfor.getA0171());
        a01s.setA0175(personInfor.getA0175());
        a01s.setA0176(personInfor.getA0176());
        a01s.setA0177a(personInfor.getA0177a());
        a01s.setA0178(personInfor.getA0178());
        a01s.setA0179(personInfor.getA0179());
        a01s.setA0183(personInfor.getA0183());
        a01s.setA0184(DateUtil.strToDate(personInfor.getA0184()));
        a01s.setA0180(DateUtil.strToDate(personInfor.getA0180()));
        a01s.setA0193(personInfor.getA0193());
        a01s.setA0196(personInfor.getA0196());
        a01s.setA0197(personInfor.getA0197());
        this.a01Mapper.insertSelective(a01s);
        return 1;
    }

    @Override // com.tdqy.cadre.modular.profess.service.PersonService
    public List<A01> selectListByUnitid(String unitid) {
        return this.a01Mapper.selectListByUnitid(unitid);
    }
}
